/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * DialogTestDriver.java
 *
 * Created on Apr 26, 2009, 10:55:53 PM
 */

package edu.ut.ece382c.valloy.ui;

import java.awt.Component;
import java.util.Collection;
import java.util.Enumeration;
import java.util.Vector;

import javax.swing.AbstractButton;
import javax.swing.SwingWorker;

import edu.ut.ece382c.valloy.translator.engine.ParserTranslator;
import edu.ut.ece382c.valloy.translator.engine.StringTranslator;
import edu.ut.ece382c.valloy.translator.engine.Translator;
import edu.ut.ece382c.valloy.translator.tester.ValloyTestDriver;

/**
 *
 * @author vasr
 */
public class DialogTestDriver extends javax.swing.JDialog {

    /** Creates new form DialogTestDriver */
    public DialogTestDriver(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
        jRadioButtonStringParser.setSelected(true);
        jRadioButtonCompileOnly.setSelected(true);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        buttonGroup1 = new javax.swing.ButtonGroup();
        buttonGroup2 = new javax.swing.ButtonGroup();
        label1 = new java.awt.Label();
        textFieldRootFolderPath = new java.awt.TextField();
        CmdRunTestDrive = new java.awt.Button();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTextArea_Result = new javax.swing.JTextArea();
        buttonClose = new java.awt.Button();
        jPanel1 = new javax.swing.JPanel();
        jRadioButtonStringParser = new javax.swing.JRadioButton();
        jRadioButtonLexerParser = new javax.swing.JRadioButton();
        jPanel2 = new javax.swing.JPanel();
        jRadioButtonCompileOnly = new javax.swing.JRadioButton();
        jRadioButtonCompileAndRun = new javax.swing.JRadioButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("FInd Test Models");

        label1.setText("Enter Root Folder Path:");

        CmdRunTestDrive.setLabel("Run Test Drive");
        CmdRunTestDrive.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                CmdRunTestDriveActionPerformed(evt);
            }
        });

        jTextArea_Result.setColumns(20);
        jTextArea_Result.setRows(5);
        jScrollPane1.setViewportView(jTextArea_Result);

        buttonClose.setLabel("Close");
        buttonClose.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buttonCloseActionPerformed(evt);
            }
        });

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("Valloy Parser"));
        jPanel1.setName("Group 1"); // NOI18N
        jPanel1.setOpaque(false);

        jRadioButtonStringParser.setText("String Parser");
        jRadioButtonStringParser.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jRadioButtonStringParserActionPerformed(evt);
            }
        });

        jRadioButtonLexerParser.setText("Lexer Parser");
        jRadioButtonLexerParser.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jRadioButtonLexerParserActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jRadioButtonStringParser)
                .addGap(18, 18, 18)
                .addComponent(jRadioButtonLexerParser)
                .addContainerGap(20, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jRadioButtonStringParser)
                    .addComponent(jRadioButtonLexerParser))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder("Verification Level"));

        jRadioButtonCompileOnly.setText("Compile Only");
        jRadioButtonCompileOnly.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jRadioButtonCompileOnlyActionPerformed(evt);
            }
        });

        jRadioButtonCompileAndRun.setText("Compile and Run");
        jRadioButtonCompileAndRun.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jRadioButtonCompileAndRunActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jRadioButtonCompileOnly)
                .addGap(18, 18, 18)
                .addComponent(jRadioButtonCompileAndRun)
                .addContainerGap(14, Short.MAX_VALUE))
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                .addComponent(jRadioButtonCompileOnly)
                .addComponent(jRadioButtonCompileAndRun))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 651, Short.MAX_VALUE)
                    .addComponent(textFieldRootFolderPath, javax.swing.GroupLayout.DEFAULT_SIZE, 651, Short.MAX_VALUE)
                    .addComponent(label1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 64, Short.MAX_VALUE)
                        .addComponent(CmdRunTestDrive, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(buttonClose, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 72, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(label1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(1, 1, 1)
                .addComponent(textFieldRootFolderPath, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(10, 10, 10)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(jPanel1, 0, 53, Short.MAX_VALUE)
                            .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(CmdRunTestDrive, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(21, 21, 21)))
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 310, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(buttonClose, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    //Action to run test drive
    private void CmdRunTestDriveActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_CmdRunTestDriveActionPerformed
        
        String rootFolderPath = textFieldRootFolderPath.getText();
       
        if(rootFolderPath.length() > 0)
        {	
        	buttonClose.setEnabled(false);
        	CmdRunTestDrive.setEnabled(false);
        	jTextArea_Result.setText("");
        	
        	//Create test-suite driver object
        	ValloyTestDriver testDriver = new ValloyTestDriver();
        	
        	//specify logging options
        	testDriver.getLogger().setControlOut(jTextArea_Result);
        	testDriver.getLogger().enableConsoleOut(true);
        	
        	//specify parser options
        	if (jRadioButtonStringParser.isSelected())
        		testDriver.setTranslator(new StringTranslator());
        	else
        		testDriver.setTranslator(new ParserTranslator());
        	
        	//specify checking options
        	if (jRadioButtonCompileAndRun.isSelected())
        		testDriver.setTestSuiteLevel(1);
        	else
        		testDriver.setTestSuiteLevel(0);
        	
        	//Create a separate thread to run suite in so that UI can be updated as it runs
        	TestSuiteWorker worker = new TestSuiteWorker();
        	
        	//list of controls to disable while the test-suite is running
        	Vector<Component> disabledControls = new Vector<Component>();
        	disabledControls.add(buttonClose);
        	disabledControls.add(CmdRunTestDrive);
        	//TODO: better way to do this? can't convert Enumeration<AbstractButton> to Vector<Component>
        	Enumeration<AbstractButton> radiobuttonList = buttonGroup1.getElements();
        	while (radiobuttonList.hasMoreElements())
        		 disabledControls.add(radiobuttonList.nextElement());
   
          	radiobuttonList = buttonGroup2.getElements();
          	while (radiobuttonList.hasMoreElements())
          		disabledControls.add(radiobuttonList.nextElement());
        	
        	worker.init(testDriver, rootFolderPath, disabledControls);
        	worker.execute();
        }

}//GEN-LAST:event_CmdRunTestDriveActionPerformed

    //Close this Dialog Window
    private void buttonCloseActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonCloseActionPerformed
        // TODO add your handling code here:
        setVisible(false);
        dispose();


    }//GEN-LAST:event_buttonCloseActionPerformed

    private void jRadioButtonLexerParserActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jRadioButtonLexerParserActionPerformed
        jRadioButtonStringParser.setSelected(false);
    }//GEN-LAST:event_jRadioButtonLexerParserActionPerformed

    private void jRadioButtonStringParserActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jRadioButtonStringParserActionPerformed
        jRadioButtonLexerParser.setSelected(false);
    }//GEN-LAST:event_jRadioButtonStringParserActionPerformed

    private void jRadioButtonCompileOnlyActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jRadioButtonCompileOnlyActionPerformed
        jRadioButtonCompileAndRun.setSelected(false);
    }//GEN-LAST:event_jRadioButtonCompileOnlyActionPerformed

    private void jRadioButtonCompileAndRunActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jRadioButtonCompileAndRunActionPerformed
        jRadioButtonCompileOnly.setSelected(false);
    }//GEN-LAST:event_jRadioButtonCompileAndRunActionPerformed

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                DialogTestDriver dialog = new DialogTestDriver(new javax.swing.JFrame(), true);
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {
                    public void windowClosing(java.awt.event.WindowEvent e) {
                        System.exit(0);
                    }
                });
                dialog.setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private java.awt.Button CmdRunTestDrive;
    private java.awt.Button buttonClose;
    private javax.swing.ButtonGroup buttonGroup1;
    private javax.swing.ButtonGroup buttonGroup2;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JRadioButton jRadioButtonCompileAndRun;
    private javax.swing.JRadioButton jRadioButtonCompileOnly;
    private javax.swing.JRadioButton jRadioButtonLexerParser;
    private javax.swing.JRadioButton jRadioButtonStringParser;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTextArea jTextArea_Result;
    private java.awt.Label label1;
    private java.awt.TextField textFieldRootFolderPath;
    // End of variables declaration//GEN-END:variables

}
